package com.auth.web.spring;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auth.domain.T_role;
import com.auth.service.T_roleService;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.common.utils.MyDateUtils;

@Controller
@RequestMapping("/t_role")

public class T_roleController
{
	private static Logger log = Logger.getLogger(T_roleController.class.getName());

	@Resource
    private T_roleService service;
	private Pg pg;

	/**
	 * 1、获取 T_role信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_role")
	@ResponseBody
	public Pg getAllT_role(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_role t_role = new T_role();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_role.setProcess_code(request.getParameter("process_code"));
	        }
			if(request.getParameter("rolename")!=null&&!"".equals(request.getParameter("rolename"))){
	        	t_role.setRolename(request.getParameter("rolename"));
	        }
			if(request.getParameter("memo")!=null&&!"".equals(request.getParameter("memo"))){
	        	t_role.setMemo(request.getParameter("memo"));
	        }
			if(request.getParameter("status")!=null&&!"".equals(request.getParameter("status"))){
	        	t_role.setStatus(request.getParameter("status"));
	        }
			if(request.getParameter("createuserid")!=null&&!"".equals(request.getParameter("createuserid"))){
	        	t_role.setCreateuserid(request.getParameter("createuserid"));
	        }
			if(request.getParameter("cretaetime")!=null&&!"".equals(request.getParameter("cretaetime"))){
	        	t_role.setCretaetime(request.getParameter("cretaetime"));
	        }
			if(request.getParameter("lastupdateuserid")!=null&&!"".equals(request.getParameter("lastupdateuserid"))){
	        	t_role.setLastupdateuserid(request.getParameter("lastupdateuserid"));
	        }
			if(request.getParameter("systemid")!=null&&!"".equals(request.getParameter("systemid"))){
	        	t_role.setSystemid(request.getParameter("systemid"));
	        }
			if(request.getParameter("ext1")!=null&&!"".equals(request.getParameter("ext1"))){
	        	t_role.setExt1(request.getParameter("ext1"));
	        }
			if(request.getParameter("ext2")!=null&&!"".equals(request.getParameter("ext2"))){
	        	t_role.setExt2(request.getParameter("ext2"));
	        }
			if(request.getParameter("ext3")!=null&&!"".equals(request.getParameter("ext3"))){
	        	t_role.setExt3(request.getParameter("ext3"));
	        }
      	    if(request.getParameter("id")!=null&&!"".equals(request.getParameter("id"))){
	        	t_role.setId(new Integer(request.getParameter("id")));
	        }

	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_role.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_role.setEnddate(request.getParameter("enddate"));
	        }
	        t_role.setStartNo( startIndex );
	        t_role.setEndNo( startIndex + pageSize );
	    	List<T_role> findAll = service.findAll(t_role);
	        int totalCount = service.findCountAll(t_role);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_roleList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_roleList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_role信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_role")
	@ResponseBody
	public JsonVO saveT_role(T_role t_role, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//设置当前操作用户信息到vo中
		t_role.setCreateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		t_role.setCretaetime(MyDateUtils.getDate());
		t_role.setLastupdateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(service.save(t_role)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 3、修改T_role信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_role
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_role")
	@ResponseBody
	public JsonVO updateT_role(T_role t_role, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//设置当前操作用户信息到vo中
		t_role.setLastupdateuserid(request.getSession().getAttribute("FREE_CURRENT_USER")+"");
		t_role.setLastupdatetime(MyDateUtils.getDate());//此处可以不设该值，因为该值是在sql中使用sysdate;
		//如果有特殊值需要处理，在此处手工转换
		//T_role t_roleTemp = service.findById(t_role);
		//t_roleTemp.setXXX(t_role.getXXX());
		//if(service.update(t_roleTemp)){

		if(service.update(t_role)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 4、根据id查询T_role单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_role json格式字符串
	 */
	@RequestMapping("/getT_role")
	@ResponseBody
	public T_role getT_role(Integer id,HttpServletRequest request){
		T_role t_role = new T_role();
		t_role.setId(id);
		return service.findById(t_role);
	}

	/**
	 * 5、删除T_role根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_role")
	@ResponseBody
	public JsonVO delT_role(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_role t_role = new T_role();
		t_role.setId(id);

		if(service.delete(t_role)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_role/toT_roleList查询所有数据页面
	 *  return /WEB-INF/views/auth/t_role/t_roleList.jsp
	 */
    @RequestMapping(value = "/toT_roleList")
    public String toT_roleList() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_role/t_roleList.jsp------");
        return "auth/t_role/t_roleList";
    }

	/**
	 * 7、新增、修改、详情页面t_role/toT_roleCurd跳转
     * auth/t_role/T_roleCurd.jsp
	 */
    @RequestMapping(value = "/toT_roleCurd")
    public String toT_roleCurd() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_role/t_roleCurd.jsp------");
        return "auth/t_role/t_roleCurd";
    }

    /**
	 * 8、批量删除T_role根据主键id
	 * 是否要实现批量删除，待考虑，基本上用途不大
	 * @param id
	 * @param request
	 * @param response
	@RequestMapping("/delT_roleBatch")
	public String delT_roleBatch(HttpServletRequest request,HttpServletResponse response){
		String[] ids = request.getParameterValues("ids");
		System.out.println("------ids="+ids);
		T_role t_role = new T_role();
		int j=0;
		for(int i =0;i< ids.length;i++){
			j++;
			log.error("------ids[i]="+ids[i]);
			t_role.setId(new Integer(ids[i]).intValue());
			service.delete(t_role);
		}
		if(j==ids.length){
			log.debug("------t_role批量删除成功！------");
		}else{
			log.error("------t_role批量删除部分成功！部分失败！------");
		}

		return "redirect:/t_role/getAllT_role";
	}
*/



}









